package com.qyhl.webtv.module_user.login.code.perfect;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.user.UserBean;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.commonlib.service.ChannelConfigService;
import com.qyhl.webtv.commonlib.utils.config.AppConfigUtil;
import com.qyhl.webtv.module_user.app.UserApplication;
import com.qyhl.webtv.module_user.common.UserUrl;
import com.qyhl.webtv.module_user.util.LoginUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class PerfectPresenterModel implements PerfectContract.PerfectPresenterModel {

    @Autowired(name = ServicePathConstant.GET_CONFIG_INFO)
    ChannelConfigService channelConfigService;


    private PerfectContract.PerfectView view;

    public PerfectPresenterModel(PerfectContract.PerfectView view) {
        ARouter.getInstance().inject(this);
        this.view = view;
    }

    @Override
    public void goLogin(String phone, String code, String nickName, String invitecode) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {

            Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.CODE_USER_LOGIN);

            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            "siteId=" + channelConfigService.getSiteId() + "&captcha=" + code + "&telphone=" + phone + "&nickname=" +
                                    nickName + "&code=" + invitecode + "&password="))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<UserBean>() {
                        @Override
                        public void onError(ApiException e) {
                            view.loginError(LoginUtils.getMessage(e.getCode()));
                        }

                        @Override
                        public void onSuccess(UserBean userBean) {
                            AppConfigUtil.getInstance().setConValue(AppConfigConstant.LOGIN_TOKEN,userBean.getToken());
                            Hawk.put("token", userBean.getToken());
                            getUserInfo(userBean.getUsername());
                        }
                    });
        } else {
            view.loginError("请检查网络...");
        }
    }


    /**
     * 获取用户信息
     *
     * @param username
     */
    private void getUserInfo(String username) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String time = sdf.format(new Date());
            String code = "Z5" + time + "I2863";
            String url = UserUrl.GET_USER_INFO_NEW + "?timestamp=" + time;

            EasyHttp.post(url)
                    .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&username=" + username))
                    .isDecrypt(true)
                    .decryptKet(code)
                    .execute(new SimpleCallBack<UserInfoBean>() {
                        @Override
                        public void onError(ApiException e) {
                            view.loginError("网络繁忙，请稍后再试");
                        }

                        @Override
                        public void onSuccess(UserInfoBean userInfoBean) {
                            view.loginSuccess(userInfoBean);
                        }
                    });
        } else {
            view.loginError("请检查网络...");
        }
    }
}
